package com.edu.lawdoc.api.interceptor;

import com.edu.lawdoc.api.controller.assistor.Result;
import com.edu.lawdoc.errors.AppException;
import com.edu.lawdoc.errors.Errors;
import com.edu.lawdoc.i18n.I18nDelegate;
import com.edu.lawdoc.api.utils.LoggerKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

public class ExceptionWithLogInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation invocation) {
        Controller controller = invocation.getController();
        /**
         * 获得请求路径
         */
        HttpServletRequest request = controller.getRequest();
        String uri = request.getRequestURI();

        try {
            invocation.invoke();
        }
        catch (AppException ae) {
            /**
             * 为什么不放在公共部分，是因为没有异常就不应该调用
             */
            String parameters = this.getParameters(request);
            /**
             * 获得系统的换行符
             * /r Mac
             * /n Unix/Linux
             * /r/n Windows
             */
            String lineSeparator = System.getProperty("line.separator");
            /**
             * 应用相关的错误
             */
            LoggerKit.error(String.format("URI: %s, " + lineSeparator
                            + " PARAMETERS: %s " + lineSeparator
                            + " AppException: %s, %s",
                    uri,
                    parameters,
                    ae.getErrorCode(),  ae.getDevelopmentDetails()));
            /**
             * 前端只关注errorCode，和errorCode对应的文字含义
             */
            Result.failure(controller, ae.getErrorCode(),
                    I18nDelegate.get(ae.getErrorCode()) + ", " + ae.getParametersDetails(), null);
        }
        catch (Exception e) {
            /**
             * 为什么不放在公共部分，是因为没有异常就不应该调用
             */
            String parameters = this.getParameters(request);

            /**
             * 获得系统的换行符
             * /r Mac
             * /n Unix/Linux
             * /r/n Windows
             */
            String lineSeparator = System.getProperty("line.separator");
            String fullStackTrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e);

            /**
             * 其他未考虑的错误
             */
            LoggerKit.error(String.format("URI: %s, " + lineSeparator
                            + " PARAMETERS: %s " + lineSeparator
                            + " Exception: %s",
                    uri,
                    parameters,
                    fullStackTrace));

            Result.failure(controller, Errors.INTERNAL_ERROR.name(), null, null);
        }
    }

    /**
     * 打印请求参数
     * @param request
     * @return
     */
    private String getParameters(HttpServletRequest request) {
        Map<String, String[]> maps =  request.getParameterMap();

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
            sb.append(entry.getKey()).append(":").append(Arrays.toString(entry.getValue())).append(";\r\n");
        }
        return sb.toString();
    }
}
